import * as forge from 'node-forge';

const PUBLIC_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmEkfC4Ro+j0/PYda4H2awRyLJ0bECqWe2/4lzy7/X6DZ5NZdEC0vPJQBBs4vGVpXwYD/Xs7l9IcX0sFuBEC/aDlIQDppc7qYhj5NATgqFXe6pGzJ/1AKWxMzxDSeB0rZTv2weZW8dJXKcGWV1HElibGcgqjtIlqiIgP728ahq69IdSR6PHjcZ2sR60VrP4hN0aUvQZFNgttKkqVuLZUlf8ZoIZsNk31Lxe8arwuWcshuApxH8hHfkxq15tQf0GAxlm4mXMgsP9/A1SVTlXK8GDOf8Oxk/dnsiOfLbQVyudH4j820ShfD1V22xwFxd1V4mXy4klBsnvK7v6ifF3TLiwIDAQAB'

function encrypt(text) {
  const pki = forge.pki;
  // 规定格式：publicKey之前需要加'-----BEGIN PUBLIC KEY-----\n'，之后需要加'\n-----END PUBLIC KEY-----'
  const publicK = pki.publicKeyFromPem('-----BEGIN PUBLIC KEY-----\n' + PUBLIC_KEY + '\n-----END PUBLIC KEY-----');
  return forge.util.encode64(publicK.encrypt(text));
}

export default {
  encrypt
}
